package com.company.project.controller;

import com.company.project.service.DeptService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysBbsEntity;
import com.company.project.service.SysBbsService;



/**
 * sys_bbs
系统留言
 *
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-08-13 17:25:28
 */
@Controller
@RequestMapping("/")
@Api(tags = "系统管理-系统留言")
public class SysBbsController {
    @Autowired
    private SysBbsService sysBbsService;

    @Autowired
    private DeptService deptService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/sysBbs")
    public String sysBbs() {
        return "sysbbs/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("sysBbs/add")
    @RequiresPermissions("sysBbs:add")
    @ResponseBody
    public DataResult add(@RequestBody SysBbsEntity sysBbs){
        sysBbs.setDeptId(deptService.rootDept());
        sysBbs.setStatus("1");
        sysBbs.setCreateTime(new Date());
        sysBbsService.save(sysBbs);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysBbs/delete")
    @RequiresPermissions("sysBbs:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        sysBbsService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysBbs/update")
    @RequiresPermissions("sysBbs:update")
    @ResponseBody
    public DataResult update(@RequestBody SysBbsEntity sysBbs){
        sysBbsService.updateById(sysBbs);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysBbs/listByPage")
    @RequiresPermissions("sysBbs:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysBbsEntity sysBbs){
        Page page = new Page(sysBbs.getPage(), sysBbs.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
        if(!StringUtils.isEmpty(sysBbs.getStatus())){
            queryWrapper.eq("status", sysBbs.getStatus());
        }
        IPage<SysBbsEntity> iPage = sysBbsService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }
    @ApiOperation(value = "阅读")
    @PutMapping("sysBbs/read")
    @RequiresPermissions("sysBbs:read")
    @ResponseBody
    public DataResult readBbs(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        List<SysBbsEntity> sysBbsEntities = new ArrayList<>();
        for (int i = 0 ; i < ids.size() ; i ++ ) {
            SysBbsEntity sysBbsEntity = new SysBbsEntity() ;
            sysBbsEntity.setId(ids.get(i));
            sysBbsEntity.setStatus(String.valueOf(0));
            sysBbsEntities.add(sysBbsEntity);
        }
        sysBbsService.updateBatchById(sysBbsEntities);

        return DataResult.success();
    }
}
